package com.health.platform.provider.internetmedicine.service;

import com.health.platform.common.core.entity.BaseQueryEntity;
import com.health.platform.common.restful.AppResponse;
import com.health.platform.provider.internetmedicine.dao.InternetMediDao;
import com.health.platform.provider.internetmedicine.entity.*;
import com.health.platform.provider.internetmedicine.utils.DictUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>互联网医疗Service</p>
 * <p>创建日期：2021-02-23</p>
 *
 * @author zhou-qf
 */
@Service
public class InternetMediService {

    @Resource
    private InternetMediDao internetMediDao;

    /**
     * 获取汇总信息
     * @param baseQueryEntity 查询条件
     * @return 结果
     */
    public AppResponse getSummaryInfo(BaseQueryEntity baseQueryEntity){
        SummaryEntity result = new SummaryEntity();

        // 查询预约挂号人次、线上诊疗人次
        SummaryEntity onlineNum = getOnlineNum(baseQueryEntity);
        if(onlineNum!=null) {
            result.setDiaNumOnline(onlineNum.getDiaNumOnline());
            result.setRegNumOnline(onlineNum.getRegNumOnline());
        }

        // 查询医疗机构数
        SummaryEntity mechanismNum = getMechanismNum(baseQueryEntity);
        result.setMechanismNum(mechanismNum.getMechanismNum());

        // 查询医生护士总数
        SummaryEntity docNurNum = getDocNurNum(baseQueryEntity);
        result.setDoctorNum(docNurNum.getDoctorNum());
        result.setNurseNum(docNurNum.getNurseNum());

        return AppResponse.success("查询成功", result);
    }

    /**
     * 获取就诊人次按机构排名
     * @param baseQueryEntity 参数
     * @return 结果
     */
    public AppResponse getVisitsNum(BaseQueryEntity baseQueryEntity){
        return AppResponse.success("查询成功", internetMediDao.getVisitsNum(baseQueryEntity));
    }

    /**
     * 获取线上疾病排名
     * @param baseQueryEntity 参数
     * @return 结果
     */
    public AppResponse getDisOnlineNum(BaseQueryEntity baseQueryEntity){
        return AppResponse.success("查询成功", internetMediDao.getDisOnlineNum(baseQueryEntity));
    }

    /**
     * 获取患者地域及年龄分布
     * @param baseQueryEntity 参数
     * @return 结果
     */
    public AppResponse getPatientAreaAge(BaseQueryEntity baseQueryEntity){
        PatientAreaAgeEntity patientAreaAgeEntity = new PatientAreaAgeEntity();
        patientAreaAgeEntity.setPatientAgeEntity(getPatientAge(baseQueryEntity));
        patientAreaAgeEntity.setPatientAreaEntity(getPatientArea(baseQueryEntity));
        return AppResponse.success("查询成功", patientAreaAgeEntity);
    }

    /**
     * 获取互联网诊疗开展情况
     * @param baseQueryEntity 参数
     * @return 结果
     */
    public AppResponse getSituationNum(BaseQueryEntity baseQueryEntity){
        List<SituationEntity> list = internetMediDao.getSituationNum(baseQueryEntity);
        /*// 设置行为名称
        for(SituationEntity situationEntity : list){
            situationEntity.setSpareName(DictUtils.getSituationName(situationEntity.getSpareCode()));
        }*/
        return AppResponse.success("查询成功", list);
    }

    /**
     * 获取线上用药排名
     * @param baseQueryEntity 参数
     * @return 结果
     */
    public AppResponse getMedicationNum(BaseQueryEntity baseQueryEntity){
        return AppResponse.success("查询成功", internetMediDao.getMedicationNum(baseQueryEntity));
    }

    /**
     * 获取预约挂号及在线就诊人次趋势
     * @param baseQueryEntity 参数
     * @return 结果
     */
    public AppResponse getNumTrend(BaseQueryEntity baseQueryEntity){
        NumTrendEntity.TrendEntity trendEntity = new NumTrendEntity.TrendEntity();
        // 预约挂号
        baseQueryEntity.setTargetType("YL_MZ_TYPE8");
        trendEntity.setRegTrend(internetMediDao.getTrendNum(baseQueryEntity));
        // 在线就诊
        baseQueryEntity.setTargetType("YL_MZ_TYPE9");
        trendEntity.setDiaTrend(internetMediDao.getTrendNum(baseQueryEntity));
        return AppResponse.success("查询成功", trendEntity);
    }

    /**
     * 获取患者地域分布
     * @param baseQueryEntity 参数
     * @return 结果
     */
    private List<PatientAreaEntity> getPatientArea(BaseQueryEntity baseQueryEntity){
        return internetMediDao.getPatientArea(baseQueryEntity);
    }

    /**
     * 获取患者年龄分布
     * @param baseQueryEntity 参数
     * @return 结果
     */
    private List<PatientAgeEntity> getPatientAge(BaseQueryEntity baseQueryEntity){
        return internetMediDao.getPatientAge(baseQueryEntity);
    }

    /**
     * 查询预约挂号人次、线上诊疗人次
     * @param baseQueryEntity 参数
     * @return 结果
     */
    private SummaryEntity getOnlineNum(BaseQueryEntity baseQueryEntity){
        return internetMediDao.getOnlineNum(baseQueryEntity);
    }

    /**
     * 查询医疗机构数
     * @param baseQueryEntity 参数
     * @return 结果
     */
    private SummaryEntity getMechanismNum(BaseQueryEntity baseQueryEntity){
        return internetMediDao.getMechanismNum(baseQueryEntity);
    }


    /**
     * 查询医生护士总数
     * @param baseQueryEntity 参数
     * @return 结果
     */
    private SummaryEntity getDocNurNum(BaseQueryEntity baseQueryEntity) {
        return internetMediDao.getDocNurNum(baseQueryEntity);
    }
    /**
     * 查询互联网医院实时诊疗情况
     * @param baseQueryEntity 参数
     * @return 结果
     */
    public  List<InternetRealTimeEntity> getSeeDoctor(BaseQueryEntity baseQueryEntity) {
        baseQueryEntity.replaceKey("t.bus_date","t.SEE_DOC_DT");
        baseQueryEntity.setStandbyStr(baseQueryEntity.getConditionStr().replaceAll("t.GBCODE", "t.ORGAN_CODE"));
        return internetMediDao.getSeeDoctor(baseQueryEntity);
    }


}
